package com.lcx.plugin.ui;

import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.intellij.openapi.ui.Messages;
import com.intellij.ui.components.JBLabel;
import com.lcx.plugin.dict.GlobalDict;
import com.lcx.plugin.dto.MySettingsStorageDTO;
import com.lcx.plugin.entity.TypeMapper;
import com.lcx.plugin.enums.MysqlTypeMatch;
import com.lcx.plugin.factory.CellEditorFactory;
import com.lcx.plugin.tool.CloneUtils;
import com.lcx.plugin.ui.component.TableComponent;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import java.util.Arrays;
import java.util.List;

/**
 * @author daxiong
 * @date 2023/6/23 下午3:14
 */
public class TypeMapperSettingForm implements MyBaseSettings {
    private final JPanel mainPanel;
    /**
     * 表格组件
     */
    private TableComponent<TypeMapper> tableComponent;
    /**
     * 元素对象
     */
    private List<TypeMapper> elementList;

    public TypeMapperSettingForm() {
        this.mainPanel = new JPanel(new BorderLayout());
    }

    private void initTable() {
        // 第一列excel类型
        TableCellEditor excelTypeEditor = CellEditorFactory.createTextFieldEditor();
        TableComponent.Column<TypeMapper> excelTypeColumn = new TableComponent.Column<>("excelType", TypeMapper::getExcelType, TypeMapper::setExcelType, excelTypeEditor, null);
        // 第二列监听输入状态，及时修改属性值
        TableCellEditor jdbcTypeEditor = CellEditorFactory.createTextFieldEditor();
        TableComponent.Column<TypeMapper> jdbcTypeColumn = new TableComponent.Column<>("jdbcType", TypeMapper::getJdbcType, TypeMapper::setJdbcType, jdbcTypeEditor, null);
        // 第三列默认长度
        TableCellEditor defaultLengthEditor = CellEditorFactory.createTextFieldEditor();
        TableComponent.Column<TypeMapper> defaultLengthColumn = new TableComponent.Column<>("defaultLength", TypeMapper::getDefaultLength, TypeMapper::setDefaultLength, defaultLengthEditor, null);

        // 表头
        List<TableComponent.Column<TypeMapper>> columns = Arrays.asList(excelTypeColumn, jdbcTypeColumn, defaultLengthColumn);
        // 表格初始化
        this.tableComponent = new TableComponent<>(columns, this.elementList, TypeMapper.class);
        this.mainPanel.add(this.tableComponent.createPanel(), BorderLayout.CENTER);
        // 上方标签显示
        this.mainPanel.add(createLabelPanel(), BorderLayout.NORTH);
    }

    public JComponent createLabelPanel() {
        JComponent labelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        labelPanel.add(new JBLabel("注意：相同excelType名称的配置会被覆盖，请谨慎修改！"));
        return labelPanel;
    }

    private void initPanel() {
        this.loadSettingsStore(getSettingsStorage());
        // 初始化表格
        this.initTable();
    }

    @Override
    public String getDisplayName() {
        return "Type Mapper";
    }

    @Override
    public @Nullable
    JComponent createComponent() {
        this.initPanel();
        return mainPanel;
    }

    @Override
    public boolean isModified() {
        return !this.elementList.equals(getSettingsStorage().getElementList());
    }

    @Override
    public void apply() {
        if (CollUtil.isEmpty(elementList)) {
            Messages.showWarningDialog("类型列表不能为空！", GlobalDict.TITLE_INFO);
            return;
        }
        for (TypeMapper item : this.elementList) {
            if (!item.paramCheckSuccess()) {
                Messages.showWarningDialog("所有表格项不能为空！", GlobalDict.TITLE_INFO);
                return;
            }
        }
        getSettingsStorage().setElementList(this.elementList);
        // 保存包后重新加载配置
        this.loadSettingsStore(getSettingsStorage());
    }

    /**
     * 加载配置信息
     *
     * @param settingsStorage 配置信息
     */
    @Override
    public void loadSettingsStore(MySettingsStorageDTO settingsStorage) {
        List<TypeMapper> elementList = settingsStorage.getElementList();
        if (CollUtil.isEmpty(elementList)) {
            // 默认值
            elementList = MysqlTypeMatch.defaultValues();
        }
        // 复制配置，防止未知的篡改
        this.elementList = CloneUtils.cloneByJson(elementList, new TypeReference<List<TypeMapper>>() {
        });
        this.refreshUiVal();
    }

    private void refreshUiVal() {
        if (this.tableComponent != null) {
            this.tableComponent.setDataList(this.elementList);
        }
    }
}
